package cn.xqj.org.controller;

import cn.xqj.basic.annotation.PreAuthorize;
import cn.xqj.basic.util.AjaxResult;
import cn.xqj.basic.util.PageList;
import cn.xqj.org.domain.Department;
import cn.xqj.org.query.DepartmentQuery;
import cn.xqj.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
// swagger 注解  用于描述接口类
@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController
@RequestMapping("/department")
/**
 * 部门接口类
 */
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 接口：通过id查询单个部门信息
     * @param id 部门id
     * @return 返回单个Department
     */
    // swagger注解  用于描述接口
    @ApiOperation(value = "通过ID查询单个部门信息" )
    @GetMapping("/{id}")
    public Department findOne(@PathVariable("id") Long id) {
        return departmentService.findOne(id);
    }


    /**
     * 接口：查询所有部门信息
     * @return 返回Department的list集合
     */
    @ApiOperation(value = "通查询所有部门信息" )
    @GetMapping
    @PreAuthorize(name = "获取所有部门",sn = "department:getAll")
    public List<Department> findAll() {
        return departmentService.findAll();
    }


    /**
     * 接口：分页查询和高级查询
     * @param departmentQuery  分页查询和高级查询的参数
     * @return 返回分页查询的数据类PageList
     */
    @ApiOperation(value = "分页查询和高级查询" )
    @PostMapping()
    @PreAuthorize(name = "部门列表",sn = "department:list") //sn后面用来做权限校验
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.pageQuery(departmentQuery);
    }


    /**
     * 接口：添加或修改部门信息（通过id判断操作  添加没有id  修改有id）
     * @param department 传入部门信息
     * @return 返回 是否成功
     */
    @ApiOperation(value = "添加和修改（添加不需要传id，修改需要传id）" )
    @PutMapping()
    @PreAuthorize(name = "获取所有部门",sn = "department:save")
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.add(department);
                return AjaxResult.me();
            } else {
                departmentService.update(department);
                return AjaxResult.me();
            }

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("服务器繁忙，请稍后再试");
        }
    }


    /**
     * 接口：通过id删除单个部门信息
     * @param id  部门id
     * @return 返回是否成功
     */
    @ApiOperation(value = "删除单个部门" )
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "获取所有部门",sn = "department:delete")
    public AjaxResult del(@PathVariable Long id) {
        try {
            departmentService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("服务器繁忙，请稍后再试");
        }
    }


    @ApiOperation(value = "批量删除部门" )
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long [] ids) {
        try {
            departmentService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("服务器繁忙，请稍后再试");
        }
    }


    @ApiOperation(value = "获取部门树" )
    @GetMapping("/departmentTree")
    public List<Department> deptTree() {
        return departmentService.deptTree();
    }


}
